package com.cz.background.common.config;

import cn.hutool.core.util.ClassUtil;
import com.cz.background.common.util.AuthUtil;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.Properties;


@Component
@Intercepts({
        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
})
public class AuditingInterceptor implements Interceptor {


    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        //获取到登录的用户名

        // 获取一个MappedStatement
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];

        // 获取SQL命令 UNKNOWN INSERT UPDATE SELECT DELETE
        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();

        // 获取参数
        Object parameter = invocation.getArgs()[1];

        if (parameter == null) {
            return invocation.proceed();
        }

        // 获取私有成员变量
        Field[] fields = FieldUtils.getAllFields(parameter.getClass());

        if (SqlCommandType.INSERT.equals(sqlCommandType)) {
            for (Field field : fields) {
                if ("createBy".equals(field.getName())) {
                    // 插入创建人
                    field.setAccessible(true);
                    field.set(parameter, AuthUtil.getUserName());
                    field.setAccessible(false);
                }

                if ("createTime".equals(field.getName())) {
                    // 插入创建时间
                    field.setAccessible(true);
                    field.set(parameter, new Date());
                    field.setAccessible(false);
                }
            }
        }

        if (SqlCommandType.UPDATE.equals(sqlCommandType)) {
            for (Field field : fields) {
                if ("updateBy".equals(field.getName())) {
                    // 插入修改人
                    field.setAccessible(true);
                    field.set(parameter,AuthUtil.getUserName());
                    field.setAccessible(false);
                }

                if ("updateTime".equals(field.getName())) {
                    // 插入修改时间
                    field.setAccessible(true);
                    field.set(parameter, new Date());
                    field.setAccessible(false);
                }
            }
        }

        return invocation.proceed();

    }

    @Override
    public Object plugin(Object target) {
        return Interceptor.super.plugin(target);
    }

    @Override
    public void setProperties(Properties properties) {
        Interceptor.super.setProperties(properties);
    }
}
